import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.ha.facecamera.configserver.pojo.EffectArea;
import com.ha.facecamera.configserver.pojo.EffectArea.EffectAreaElement;

public class SetEffectAreaDialog extends JDialog implements ActionListener {

	private static final long serialVersionUID = 7773158608043070444L;
	private final JPanel contentPanel = new JPanel();
	private JCheckBox[] chk_enable_group = new JCheckBox[5];				// 每个规则是否启用
	private JCheckBox[][][] chk_enable_segment = new JCheckBox[5][7][6];	// 每个时间段是否启用
	private JTextField[] txt_no = new JTextField[5];						// 模式编号
	private JTextField[] txt_name = new JTextField[5];						// 模式名称
	private JRadioButton[][] rb_mode = new JRadioButton[5][2];				// 每个规则的调度模式
	private JTextField[][][] txt_hour_start = new JTextField[5][7][6];		// 每个规则下每天每个时间段的起始时间的小时
	private JTextField[][][] txt_minute_start = new JTextField[5][7][6];	// 每个规则下每天每个时间段的起始时间的分钟
	private JTextField[][][] txt_hour_end = new JTextField[5][7][6];		// 每个规则下每天每个时间段的结束时间的小时
	private JTextField[][][] txt_minute_end = new JTextField[5][7][6];		// 每个规则下每天每个时间段的结束时间的分钟

	private EffectArea[] effectAreas;
	public void setData(EffectArea[] _effectAreas) {
		m2v(_effectAreas);
	}
	
	public EffectArea[] getData() {
		return effectAreas;
	}
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SetEffectAreaDialog dialog = new SetEffectAreaDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			EffectArea[] effectAreas = new EffectArea[1];
			effectAreas[0] = new EffectArea();
			effectAreas[0].setMode((short) 1);
			effectAreas[0].setNo((short) 1);
			effectAreas[0].setName("123g3a");
			effectAreas[0].setEles(new EffectAreaElement[1]);
			effectAreas[0].getEles()[0] = effectAreas[0].new EffectAreaElement();
			effectAreas[0].getEles()[0].setSegCount(3);
			effectAreas[0].getEles()[0].setAreaStarts(new LocalTime[] {LocalTime.of(7, 0), LocalTime.of(11, 30), LocalTime.of(5, 30)});
			effectAreas[0].getEles()[0].setAreaEnds(new LocalTime[] {LocalTime.of(8, 0), LocalTime.of(12, 0), LocalTime.of(6, 0)});
			dialog.setData(effectAreas);
			dialog.setVisible(true);
			System.out.println(Arrays.toString(dialog.getData()));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SetEffectAreaDialog() {
		setModal(true);
		setBounds(100, 100, 860, 526);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
			contentPanel.add(tabbedPane);
			
			tabbedPane.addTab("规则1", null, createEffectArea(0), null);
			tabbedPane.addTab("规则2", null, createEffectArea(1), null);
			tabbedPane.addTab("规则3", null, createEffectArea(2), null);
			tabbedPane.addTab("规则4", null, createEffectArea(3), null);
			tabbedPane.addTab("规则5", null, createEffectArea(4), null);
			
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("设 置");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("取 消");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);
				buttonPane.add(cancelButton);
			}
		}
	}

	private JLayeredPane createEffectArea(int gno) {
		JLayeredPane layeredPane = new JLayeredPane();
		layeredPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		layeredPane.add(panel, BorderLayout.NORTH);
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JCheckBox checkBox_1 = new JCheckBox("启用");
		panel.add(checkBox_1);
		chk_enable_group[gno] = checkBox_1;
		
		JLabel label_1 = new JLabel("调度方式：");
		
		
		JLabel label_2 = new JLabel("编号：");
		panel.add(label_2);
		
		JTextField textField_1 = new JTextField();
		panel.add(textField_1);
		textField_1.setColumns(3);
		txt_no[gno] = textField_1;
		
		JLabel label_3 = new JLabel("名称：");
		panel.add(label_3);
		
		JTextField textField_2 = new JTextField();
		panel.add(textField_2);
		textField_2.setColumns(10);
		txt_name[gno] = textField_2;
		panel.add(label_1);
		ButtonGroup group = new ButtonGroup();
		JRadioButton radioButton_1 = new JRadioButton("按天");
		panel.add(radioButton_1);
		group.add(radioButton_1);
		rb_mode[gno][0] = radioButton_1;
		
		JRadioButton radioButton_2 = new JRadioButton("按周");
		panel.add(radioButton_2);
		group.add(radioButton_2);
		rb_mode[gno][1] = radioButton_2;
		
		JLabel label = new JLabel("（按天调度时只需设置周日的时间段）");
		panel.add(label);
		
		JTabbedPane tabbedPane_1 = new JTabbedPane(JTabbedPane.TOP);
		layeredPane.add(tabbedPane_1, BorderLayout.CENTER);

		tabbedPane_1.addTab("周日", null, createEffectAreaElement(gno, 0), null);
		tabbedPane_1.addTab("周一", null, createEffectAreaElement(gno, 1), null);
		tabbedPane_1.addTab("周二", null, createEffectAreaElement(gno, 2), null);
		tabbedPane_1.addTab("周三", null, createEffectAreaElement(gno, 3), null);
		tabbedPane_1.addTab("周四", null, createEffectAreaElement(gno, 4), null);
		tabbedPane_1.addTab("周五", null, createEffectAreaElement(gno, 5), null);
		tabbedPane_1.addTab("周六", null, createEffectAreaElement(gno, 6), null);
		
		return layeredPane;
	}
	
	private JLayeredPane createEffectAreaElement(int gno, int dno) {
		JLayeredPane layeredPane_1 = new JLayeredPane();
		layeredPane_1.setLayout(new GridLayout(0, 1, 0, 0));
		
		JPanel panel_44 = new JPanel();
		layeredPane_1.add(panel_44);
		
		JCheckBox checkBox_44 = new JCheckBox("启用");
		panel_44.add(checkBox_44);
		chk_enable_segment[gno][dno][0] = checkBox_44;
		
		JLabel label_172 = new JLabel("时间区间：");
		panel_44.add(label_172);
		
		JTextField textField_336 = new JTextField();
		textField_336.setColumns(10);
		panel_44.add(textField_336);
		txt_hour_start[gno][dno][0] = textField_336;
		
		JLabel label_173 = new JLabel(":");
		panel_44.add(label_173);
		
		JTextField textField_337 = new JTextField();
		textField_337.setColumns(10);
		panel_44.add(textField_337);
		txt_minute_start[gno][dno][0] = textField_337;
		
		JLabel label_174 = new JLabel("~");
		panel_44.add(label_174);
		
		JTextField textField_338 = new JTextField();
		textField_338.setColumns(10);
		panel_44.add(textField_338);
		txt_hour_end[gno][dno][0] = textField_338;
		
		JLabel label_175 = new JLabel(":");
		panel_44.add(label_175);
		
		JTextField textField_339 = new JTextField();
		textField_339.setColumns(10);
		panel_44.add(textField_339);
		txt_minute_end[gno][dno][0] = textField_339;
		
		JPanel panel_45 = new JPanel();
		layeredPane_1.add(panel_45);
		
		JCheckBox checkBox_45 = new JCheckBox("启用");
		panel_45.add(checkBox_45);
		chk_enable_segment[gno][dno][1] = checkBox_45;
		
		JLabel label_176 = new JLabel("时间区间：");
		panel_45.add(label_176);
		
		JTextField textField_340 = new JTextField();
		textField_340.setColumns(10);
		panel_45.add(textField_340);
		txt_hour_start[gno][dno][1] = textField_340;
		
		JLabel label_177 = new JLabel(":");
		panel_45.add(label_177);
		
		JTextField textField_341 = new JTextField();
		textField_341.setColumns(10);
		panel_45.add(textField_341);
		txt_minute_start[gno][dno][1] = textField_341;
		
		JLabel label_178 = new JLabel("~");
		panel_45.add(label_178);
		
		JTextField textField_342 = new JTextField();
		textField_342.setColumns(10);
		panel_45.add(textField_342);
		txt_hour_end[gno][dno][1] = textField_342;
		
		JLabel label_179 = new JLabel(":");
		panel_45.add(label_179);
		
		JTextField textField_343 = new JTextField();
		textField_343.setColumns(10);
		panel_45.add(textField_343);
		txt_minute_end[gno][dno][1] = textField_343;
		
		JPanel panel_46 = new JPanel();
		layeredPane_1.add(panel_46);
		
		JCheckBox checkBox_46 = new JCheckBox("启用");
		panel_46.add(checkBox_46);
		chk_enable_segment[gno][dno][2] = checkBox_46;
		
		JLabel label_180 = new JLabel("时间区间：");
		panel_46.add(label_180);
		
		JTextField textField_344 = new JTextField();
		textField_344.setColumns(10);
		panel_46.add(textField_344);
		txt_hour_start[gno][dno][2] = textField_344;
		
		JLabel label_181 = new JLabel(":");
		panel_46.add(label_181);
		
		JTextField textField_345 = new JTextField();
		textField_345.setColumns(10);
		panel_46.add(textField_345);
		txt_minute_start[gno][dno][2] = textField_345;
		
		JLabel label_182 = new JLabel("~");
		panel_46.add(label_182);
		
		JTextField textField_346 = new JTextField();
		textField_346.setColumns(10);
		panel_46.add(textField_346);
		txt_hour_end[gno][dno][2] = textField_346;
		
		JLabel label_183 = new JLabel(":");
		panel_46.add(label_183);
		
		JTextField textField_347 = new JTextField();
		textField_347.setColumns(10);
		panel_46.add(textField_347);
		txt_minute_end[gno][dno][2] = textField_347;
		
		JPanel panel_47 = new JPanel();
		layeredPane_1.add(panel_47);
		
		JCheckBox checkBox_47 = new JCheckBox("启用");
		panel_47.add(checkBox_47);
		chk_enable_segment[gno][dno][3] = checkBox_47;
		
		JLabel label_184 = new JLabel("时间区间：");
		panel_47.add(label_184);
		
		JTextField textField_348 = new JTextField();
		textField_348.setColumns(10);
		panel_47.add(textField_348);
		txt_hour_start[gno][dno][3] = textField_348;
		
		JLabel label_185 = new JLabel(":");
		panel_47.add(label_185);
		
		JTextField textField_349 = new JTextField();
		textField_349.setColumns(10);
		panel_47.add(textField_349);
		txt_minute_start[gno][dno][3] = textField_349;
		
		JLabel label_186 = new JLabel("~");
		panel_47.add(label_186);
		
		JTextField textField_350 = new JTextField();
		textField_350.setColumns(10);
		panel_47.add(textField_350);
		txt_hour_end[gno][dno][3] = textField_350;
		
		JLabel label_187 = new JLabel(":");
		panel_47.add(label_187);
		
		JTextField textField_351 = new JTextField();
		textField_351.setColumns(10);
		panel_47.add(textField_351);
		txt_minute_end[gno][dno][3] = textField_351;
		
		JPanel panel_48 = new JPanel();
		layeredPane_1.add(panel_48);
		
		JCheckBox checkBox_48 = new JCheckBox("启用");
		panel_48.add(checkBox_48);
		chk_enable_segment[gno][dno][4] = checkBox_48;
		
		JLabel label_188 = new JLabel("时间区间：");
		panel_48.add(label_188);
		
		JTextField textField_352 = new JTextField();
		textField_352.setColumns(10);
		panel_48.add(textField_352);
		txt_hour_start[gno][dno][4] = textField_352;
		
		JLabel label_189 = new JLabel(":");
		panel_48.add(label_189);
		
		JTextField textField_353 = new JTextField();
		textField_353.setColumns(10);
		panel_48.add(textField_353);
		txt_minute_start[gno][dno][4] = textField_353;
		
		JLabel label_190 = new JLabel("~");
		panel_48.add(label_190);
		
		JTextField textField_354 = new JTextField();
		textField_354.setColumns(10);
		panel_48.add(textField_354);
		txt_hour_end[gno][dno][4] = textField_354;
		
		JLabel label_191 = new JLabel(":");
		panel_48.add(label_191);
		
		JTextField textField_355 = new JTextField();
		textField_355.setColumns(10);
		panel_48.add(textField_355);
		txt_minute_end[gno][dno][4] = textField_355;
		
		JPanel panel_49 = new JPanel();
		layeredPane_1.add(panel_49);
		
		JCheckBox checkBox_49 = new JCheckBox("启用");
		panel_49.add(checkBox_49);
		chk_enable_segment[gno][dno][5] = checkBox_49;
		
		JLabel label_192 = new JLabel("时间区间：");
		panel_49.add(label_192);
		
		JTextField textField_356 = new JTextField();
		textField_356.setColumns(10);
		panel_49.add(textField_356);
		txt_hour_start[gno][dno][5] = textField_356;
		
		JLabel label_193 = new JLabel(":");
		panel_49.add(label_193);
		
		JTextField textField_357 = new JTextField();
		textField_357.setColumns(10);
		panel_49.add(textField_357);
		txt_minute_start[gno][dno][5] = textField_357;
		
		JLabel label_194 = new JLabel("~");
		panel_49.add(label_194);
		
		JTextField textField_358 = new JTextField();
		textField_358.setColumns(10);
		panel_49.add(textField_358);
		txt_hour_end[gno][dno][5] = textField_358;
		
		JLabel label_195 = new JLabel(":");
		panel_49.add(label_195);
		
		JTextField textField_359 = new JTextField();
		textField_359.setColumns(10);
		panel_49.add(textField_359);
		txt_minute_end[gno][dno][5] = textField_359;
		
		return layeredPane_1;
	}

	private void m2v(EffectArea[] effectAreas) {
		if(effectAreas != null && effectAreas.length > 0) {
			for(int i = 0; i < effectAreas.length; ++i) {
				EffectArea effectArea = effectAreas[i];
				chk_enable_group[i].setSelected(true);
				txt_no[i].setText(String.valueOf(effectArea.getNo()));
				txt_name[i].setText(effectArea.getName());
				if(effectArea.getMode() == 1)
					rb_mode[i][0].setSelected(true);
				else
					rb_mode[i][1].setSelected(true);
				for(int j = 0; j < effectArea.getEles().length; ++j) {
					EffectAreaElement effectAreaElement = effectArea.getEles()[j];
					if(effectAreaElement.getSegCount() > 0) {
						for(int k = 0; k < effectAreaElement.getSegCount(); ++k) {
							chk_enable_segment[i][j][k].setSelected(true);
							txt_hour_start[i][j][k].setText(String.valueOf(effectAreaElement.getAreaStarts()[k].getHour()));
							txt_hour_end[i][j][k].setText(String.valueOf(effectAreaElement.getAreaEnds()[k].getHour()));
							txt_minute_start[i][j][k].setText(String.valueOf(effectAreaElement.getAreaStarts()[k].getMinute()));
							txt_minute_end[i][j][k].setText(String.valueOf(effectAreaElement.getAreaEnds()[k].getMinute()));
						}
					}
				}
			}
		}
	}
	private void v2m() {
		effectAreas = IntStream.range(0, 5).filter(i -> chk_enable_group[i].isSelected()).mapToObj(i -> {
			EffectArea ret = new EffectArea();
			ret.setNo(Short.parseShort(txt_no[i].getText()));
			ret.setName(txt_name[i].getText());
			ret.setMode((short) (rb_mode[i][0].isSelected() ? 1 : 2));
			if(ret.getMode() == 1) {
				if(Arrays.stream(chk_enable_segment[i][0]).filter(JCheckBox::isSelected).count() < 1) return null; // 按天，但周日没有启用时间区间
				ret.setEles(new EffectAreaElement[1]);
				ret.getEles()[0] = ret.new EffectAreaElement();
				ret.getEles()[0].setSegCount((int) Arrays.stream(chk_enable_segment[i][0]).filter(JCheckBox::isSelected).count());
				List<LocalTime> starts = new ArrayList<>(6);
				List<LocalTime> ends = new ArrayList<>(6);
				IntStream.range(0, 6).filter(j -> chk_enable_segment[i][0][j].isSelected()).forEach(j -> {
					starts.add(LocalTime.of(Integer.parseInt(txt_hour_start[i][0][j].getText()), Integer.parseInt(txt_minute_start[i][0][j].getText())));
					ends.add(LocalTime.of(Integer.parseInt(txt_hour_end[i][0][j].getText()), Integer.parseInt(txt_minute_end[i][0][j].getText())));
				});
				ret.getEles()[0].setAreaStarts(starts.toArray(new LocalTime[0]));
				ret.getEles()[0].setAreaEnds(ends.toArray(new LocalTime[0]));
			} else {
				if(Arrays.stream(chk_enable_segment[i]).flatMap(Arrays::stream).filter(JCheckBox::isSelected).count() < 1) return null; // 按周，但没有启用时间区间
				List<EffectAreaElement> effectAreaElements = new ArrayList<>(7);
				IntStream.range(0, 7)/*.filter(j -> Arrays.stream(chk_enable_segment[i][j]).anyMatch(JCheckBox::isSelected))*/.forEach(j -> {
					EffectAreaElement effectAreaElement = ret.new EffectAreaElement();
					effectAreaElement.setSegCount((int) Arrays.stream(chk_enable_segment[i][j]).filter(JCheckBox::isSelected).count());
					effectAreaElements.add(effectAreaElement);
					if(effectAreaElement.getSegCount() < 1) return;
					List<LocalTime> starts = new ArrayList<>(6);
					List<LocalTime> ends = new ArrayList<>(6);
					IntStream.range(0, 6).filter(k -> chk_enable_segment[i][j][k].isSelected()).forEach(k -> {
						starts.add(LocalTime.of(Integer.parseInt(txt_hour_start[i][j][k].getText()), Integer.parseInt(txt_minute_start[i][j][k].getText())));
						ends.add(LocalTime.of(Integer.parseInt(txt_hour_end[i][j][k].getText()), Integer.parseInt(txt_minute_end[i][j][k].getText())));
					});
					effectAreaElement.setAreaStarts(starts.toArray(new LocalTime[0]));
					effectAreaElement.setAreaEnds(ends.toArray(new LocalTime[0]));
				});;
				ret.setEles(effectAreaElements.toArray(new EffectAreaElement[0]));
			}
			return ret;
		}).filter(obj -> obj != null).toArray(EffectArea[]::new);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("Cancel"))
			effectAreas = null;
		if(e.getActionCommand().equals("OK"))
			v2m();
		setVisible(false);		
	}
}
